package com.ieds.modules.common.Util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.security.MessageDigest;

public class StringUtil {

    public static String encodePassword(String password, String algorithm) {
        byte[] unencodedPassword = password.getBytes();
        MessageDigest md = null;

        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (Exception var7) {
            return password;
        }

        md.reset();
        md.update(unencodedPassword);
        byte[] encodedPassword = md.digest();
        StringBuffer buf = new StringBuffer();

        for(int i = 0; i < encodedPassword.length; ++i) {
            if ((encodedPassword[i] & 255) < 16) {
                buf.append("0");
            }

            buf.append(Long.toString((long)(encodedPassword[i] & 255), 16));
        }

        return buf.toString();
    }

    public static String encodeString(String str) {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encodeBuffer(str.getBytes()).trim();
    }

    public static String decodeString(String str) {
        BASE64Decoder dec = new BASE64Decoder();

        try {
            return new String(dec.decodeBuffer(str));
        } catch (IOException var3) {
            throw new RuntimeException(var3.getMessage(), var3.getCause());
        }
    }

    public static String washString(String str) {
        byte[] buffer = str.getBytes();
        byte[] buffernew = new byte[buffer.length];
        int j = 0;

        for(int i = 0; i < buffer.length; ++i) {
            if (buffer[i] >= 20 || buffer[i] < 0) {
                buffernew[j++] = buffer[i];
            }
        }

        String newStr = new String(buffernew);
        newStr = newStr.replaceAll("FORMTEXT", "");
        return newStr.trim();
    }
}
